﻿/*
* (c) by Andreas Kreisl
*
* Sys Monitor - Plugin: Physical Memory
* Version: 0.1
*
* License: creative commons BY-NC-SA
*/


// --------------------------------------------------------------------------------
// myPlugin Variables (required!)

myPlugins["mem"] = new Object();
myPlugins["mem"]["name"] = getLocalizedString2("mod_sysm_plugin_mem_name");
myPlugins["mem"]["onlyonce"] = true;
myPlugins["mem"]["newObject"] = function (NewSettings) { return new plugin_mem_sensor(NewSettings); }

// --------------------------------------------------------------------------------
// The Sensore Object

function plugin_mem_sensor(NewSettings) {
	// Required variables
	this.plugin = "mem";
	this.settings = NewSettings;
	this.edit = plugin_mem_edit;
	this.ReDraw = plugin_mem_ReDraw;
	this.frame = new Frame();
	this.frame.window = main;
	this.frame.width = mod_sysmonitor_width;


	//Init Settings
	if(!this.settings.color) this.settings.color = "#FFFFFF";
	if(!this.settings.barcolor) this.settings.barcolor = "#008000";
	if(!this.settings.updspeed) this.settings.updspeed = 15;


	//The Sonsore
	this.phys = new class_PercentBar(this.frame,0,0,getLocalizedString2("mod_sysm_plugin_mem_label"));
	this.phys.PercentColor(this.settings.color);
	this.phys.PercentBarColor(this.settings.barcolor);


	// Update timer
	var obj = this;
	this.timer = new Timer();
	this.timer.interval = this.settings.updspeed;
	this.timer.ticking = 1;
	this.timer.onTimerFired = function ( ) { plugin_mem_update.call(obj); };
	plugin_mem_update.call(obj);


	// Calculating sensore height (required!)
	this.frame.height = this.phys.background.vOffset + this.phys.background.height;
	return true;

// --------------------------------------------------------------------------------
// Helper Functions

	function plugin_mem_edit() {
		var Drive_Edit_DialogFields = Array();
		var FieldID = -1;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_color");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.color;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_barcolor");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.barcolor;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_mem_pref_text");
		Drive_Edit_DialogFields[FieldID].type = 'popup';
		Drive_Edit_DialogFields[FieldID].option = new Array(getLocalizedString2("mod_sysm_plugin_mem_pref_free"),getLocalizedString2("mod_sysm_plugin_mem_pref_used"),getLocalizedString2("mod_sysm_plugin_mem_pref_total"));
		Drive_Edit_DialogFields[FieldID].optionValue = new Array(0,1,2);
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.text;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_updatespeed_label");
		Drive_Edit_DialogFields[FieldID].type = 'popup';
		Drive_Edit_DialogFields[FieldID].optionValue = new Array(1,2,3,5,10,15);
		Drive_Edit_DialogFields[FieldID].option = new Array();
		for(var i = 0; i < Drive_Edit_DialogFields[FieldID].optionValue.length ; i++ )
			Drive_Edit_DialogFields[FieldID].option[i] = getLocalizedString2("mod_sysm_plugin_updatespeed_" + Drive_Edit_DialogFields[FieldID].optionValue[i]);
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.updspeed;


		var Drive_Edit_DialogResults = form(Drive_Edit_DialogFields, getLocalizedString2("mod_sysm_plugin_preftitle") + myPlugins[this.plugin]["name"]);
		if (Drive_Edit_DialogResults != null) {
			this.settings.color = Drive_Edit_DialogResults[0];
			this.phys.PercentColor(this.settings.color);

			this.settings.barcolor = Drive_Edit_DialogResults[1];
			this.phys.PercentBarColor(this.settings.barcolor);

			this.settings.text = Drive_Edit_DialogResults[2];

			if ( this.settings.updspeed != Drive_Edit_DialogResults[3] ) {
				this.settings.updspeed = Drive_Edit_DialogResults[3];
				this.timer.interval = this.settings.updspeed;
			}

			mod_sysmonitor_ReDraw();
			plugin_mem_update.call(this);
		}
		delete Drive_Edit_DialogFields;
		return true;
	}

	function plugin_mem_update() {
		if(this.settings.text==1)
			this.phys.PercentBarValue( 100*(system.memory.totalPhysical - system.memory.availPhysical)/system.memory.totalPhysical ,bytesToUIString( system.memory.totalPhysical - system.memory.availPhysical ));
		else if(this.settings.text==2)
			this.phys.PercentBarValue( 100*(system.memory.totalPhysical - system.memory.availPhysical)/system.memory.totalPhysical ,bytesToUIString( system.memory.totalPhysical ));
		else
			this.phys.PercentBarValue( 100*(system.memory.totalPhysical - system.memory.availPhysical)/system.memory.totalPhysical ,bytesToUIString( system.memory.availPhysical ));
		return true;
	}

	function plugin_mem_ReDraw() {
		this.frame.width = mod_sysmonitor_width;
		this.phys.style = preferences.mod_sysmonitor_bar_style.value;
		this.phys.size = mod_sysmonitor_size;
		this.frame.height = this.phys.background.vOffset + this.phys.background.height;
		return true;
	}

}

// --------------------------------------------------------------------------------
// The END